
#ifndef _WEBCOLLECTOR_H_
#define _WEBCOLLECTOR_H_

#include <QObject>
#include <json/json.h>
#include "webactivite.h"
#include "webspideraround.h"
#include "../custom/webspidercustom.h"

#define MAX_ACTIVITE_COUNT  20
#define MID_ACTIVITE_COUNT  10
#define WAR_ACTIVITE_COUNT  5

#define LIMIT_FDCOUNT 1000
#define MAX_FDCOUNT 900
#define MID_FDCOUNT 700
#define WAR_FDCOUNT 600

#define SLEEPING_TIMER_TIME 10

#define MIN_MEMORY_LIMIT 0.0
#define LIMIT_MEMORY_LIMIT 0.0

#define HAS_WORKED_LIMIT 10000
#define HAS_WORKED_LIMIT_PREPARE 9980

#define EXIT_WORK_COUNT 1000

#define DEFAULT_SELF_DESTROY_TIME (60*60*100)

#define UPDATE_PROXY_TIME 60000

class WebCollector : public QObject {
    Q_OBJECT
    public:
        ~WebCollector();

        void setProxy(QString proxyType,
                QString proxyAuthUser, QString proxyAuthPass, QString proxyHost, int proxyPort);
        void enableProxy();
        void disableProxy();

        void insert(WebSpider *spider);
        WebSpider* getSpider(QString pos);

        static WebCollector *instance();

        WebActivite* getActiviteByOpt(Operator *opt);

        double getSystemFreeRamPercent();
        bool checkPPP0State();

        QList<QString> getRunningUrls();

        void getPageInfos(ProcessRouter::CorePages *pages);
        int getRunningActiviteCount();
        int getWaitActiviteCount();
        int getHasWorkedCount();

        int currentPid() { return m_pid; }

        void killSelf();
        void exitSelf();

        bool newActivite(WebSpiderRequest *request);
        void removeActiviteByOpt(Operator *opt);
        Operator* createActiviteWithCompletedWebPage(WebPage *page);

        void setDebug();
        bool debug();

        void setStatic();
        bool isStatic();

        void setExitWorkCount(int count);
        int exitWorkCount();

    protected:
        void timerEvent(QTimerEvent *event);

    private Q_SLOTS:
        void updateProxy();

    private:
        WebCollector();

        WebActivite* createActivite(WebSpiderRequest *req);

        void scheduLoop(QTimerEvent *event);
        void scheduActivites();

        void startUpdateProxyTimer();
        void stopUpdateProxyTimer();

        static WebCollector* m_instance;

        Log4Qt::Logger *m_logger;
        QMap<QString, WebSpider*> m_spiders;
        std::list<WebActivite*> m_waitActivites;
        QList<WebActivite*> m_startsActivites;
        QList<WebActivite*> m_removeActivites;

        QTimer *m_proxyUpdateTimer;

        QString m_proxyType;
        QString m_proxyAuthUser;
        QString m_proxyAuthPass;
        QString m_proxyHost;
        int m_proxyPort;

        int m_maxActiviteCount;
        int m_pid;
        int m_ppid;
        unsigned long m_systemFreeRam;
        bool m_isSleeping;
        int m_hasWorkedCount;
        bool m_hasWorkedOutLimit;
		bool m_dying;
        int m_timerId;
        bool m_hasDeleteLater;
        int m_allowStartCount;
        int m_scheduBCount;
        bool m_isDebug;
        bool m_isStatic;
        int m_exitWorkCount;
        bool m_enableProxy;
};

#endif

